/*******************************************************
> Copyright (C) 2024 ==iot== All rights reserved.
> File Name: 4.c
> Author: wyf
> Created Time: 2024年09月02日 星期一 17时12分23秒
***************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define IP "192.168.1.128"
#define SM "255.255.15.0"
int get_result(const int* dest_ip, const int* dest_sm, int* result);
int string_to_integer(char* buf, int* dest);

int main(int argc, const char* argv[])
{
    char ip_buf[20] = { IP };//ip地址
    char sm_buf[20] = { SM };//子网掩码
    int dest_ip[20] = { 0 };//整数ip
    int dest_sm[20] = { 0 };//整数sm
    int result[20] = { 0 };
    string_to_integer(ip_buf, dest_ip);
    string_to_integer(sm_buf, dest_sm);
    get_result(dest_ip, dest_sm, result);

    for (int i = 0; i < 4; i++) {
        printf("%d ", result[i]);
    }

    return 0;
}
//把字符串转换成整数
int string_to_integer(char* buf,int *dest)
{
    char* p = NULL;
    char* temp = ".";
    int i = 0;
    //ip拆分
    p = strtok(buf, temp);
    dest[0] = atoi(p);
    while (p = strtok(NULL, temp)) {
        dest[++i] = atoi(p);
    }
    //ip拆分的四个字段数字 
    printf("%d %d %d %d\n", dest[0], dest[1], dest[2], dest[3]);
    return 0;
}


//对应字段按位与 a & b,再存进数组
int get_result(const int* dest_ip,const int* dest_sm,int* result)
{
    int i = 0;
    memset(result, 0, sizeof(result));
    for (i = 0; i < 4; i++){
        result[i] = (dest_ip[i] & dest_sm[i]);
    }

    return 0;
}


